package com.fsc.controller;


import com.fsc.entity.domain.ExamsInfo;
import com.fsc.service.ExamsInfoService;
import com.fsc.service.TotalScoreInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/exams/")
public class ExamsController {

	@Autowired
  	private ExamsInfoService examsInfoService;

	@Autowired
	private TotalScoreInfoService totalScoreInfoService;

	@GetMapping("list")
	public ResponseEntity<List<ExamsInfo>> listAllExams() {
		List<ExamsInfo> exams = examsInfoService.getAllExams();
		return ResponseEntity.ok(exams);
	}
	/*
	增加考试名称
	 */
	@GetMapping("add")
	public ResponseEntity<Integer> addExams(String examsName) {
		ExamsInfo examsInfo = new ExamsInfo();
		examsInfo.setTitle(examsName);
		examsInfo.setDate(Date.valueOf(LocalDate.now()));
		Integer id = examsInfoService.addExams(examsInfo);
		return ResponseEntity.ok(id);
	}


	/**
	 * 根据id删除成绩
	 */
	@DeleteMapping("/delete/{id}")
	public  ResponseEntity deleteExams(@PathVariable Integer id){
		totalScoreInfoService.deleteByExamsId(id);
		examsInfoService.removeById(id);

		return ResponseEntity.ok("删除成功");
	}

}
